package org.jeecg.modules.AccessoriesCenter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.AccessoriesCenter.entity.ButtonDaochuEntity;
import org.jeecg.modules.AccessoriesCenter.entity.ButtonEntity;
import org.jeecg.modules.AccessoriesCenter.mapper.ButtonManageMapper;
import org.jeecg.modules.AccessoriesCenter.service.ButtonManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ButtonManageServiceImpl implements ButtonManageService {

    @Autowired
    ButtonManageMapper mapper;

    @Override
    public IPage<ButtonEntity> getButtonlist(Page<ButtonEntity> page, Integer status, String buttonsName, String buttonsCode, String categoryId) {
        return mapper.getButtonlist(page,  status,  buttonsName,  buttonsCode,categoryId);
    }

    @Override
    public int updateButtonstatus(int id, int status) {
        return mapper.updateButtonstatus( id,  status);
    }

    @Override
    public ButtonEntity getButtonbyId(int id) {
        return mapper.getButtonbyId(id);
    }

    @Override
    public int addButton(ButtonEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addButton(entity);
    }

    /**
     * 查询纽扣编号是否已经存在
     * @param buttonsCode
     * @return
     */
    @Override
    public String getButtoncodebycode(String buttonsCode) {
        return mapper.getButtoncodebycode(buttonsCode);
    }

    @Override
    public int updateButton(ButtonEntity entity) {
        return mapper.updateButton(entity);
    }

    /**
     * 查询所有的纽扣品类
     * @return
     */
    @Override
    public List<ButtonEntity> getallButtonCategory() {
        return mapper.getallButtonCategory();
    }


    /**
     * 批量修改状态，批量上下架
     * @param ids
     * @param status
     */
    @Override
    public void piliangUpdatestatus( List<Integer> ids, int status) {
         for(int i=0;i<ids.size();i++){
             mapper.updateButtonstatus(ids.get(i),status);
         }
    }

    @Override
    public List<ButtonDaochuEntity> getButtonDaochulist(Integer status, String buttonsCode, String categoryId) {
        return mapper.getButtonDaochulist( status,  buttonsCode,  categoryId);
    }

    @Override
    public int importExcel(List<ButtonDaochuEntity> list) {

       if(list!=null){
           for( int i=0;i<list.size();i++){
               list.get(i).setCreateTime(new Date());

               String oldcunzai=getButtoncodebycode( list.get(i).getButtonsCode());
               //如果不存在 则执行插入操作
               if(oldcunzai==null){

                   //通过纽扣名称查询品类Id
                   int categoryId=mapper.getcategoryIdbyName(list.get(i).getCategoryName());
                   list.get(i).setCategoryId(String.valueOf(categoryId));
                //  String sysuserId=mapper.getSysuserIdbyworkNo(list.get(i).getWorkNo());
                   String sysuserId=mapper.getSysuserIdbySysuserName(list.get(i).getRealname());
                  list.get(i).setSysuserId(sysuserId);
                  if(sysuserId!=null)
                  mapper.addExcelButton(list.get(i));
               }else{
                   //通过纽扣名称查询品类Id
                   int categoryId=mapper.getcategoryIdbyName(list.get(i).getCategoryName());
                   list.get(i).setCategoryId(String.valueOf(categoryId));
                   String sysuserId=mapper.getSysuserIdbySysuserName(list.get(i).getRealname());
                   list.get(i).setSysuserId(sysuserId);
                    mapper.updateExcelButton(list.get(i));
               }



           }
       }
        return 1;
    }
}
